﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using MQTT.Server.Host.Controller;
using MQTT.Server.Host.Entity;
using MQTT.Server.Host.WebAPI.Models;

namespace MQTT.Server.Host.WebAPI.Controller
{
    public class WarningController : ApiController
    {
        [HttpGet]
        [ActionName("list")]
        public ApiResponse List()
        {
            ApiResponse response = new ApiResponse();
            try
            {
                List<WarningInfoEntity> infos = DataController.GetWarningInfos();
                response.Data = infos;
                response.Code = ResponseCode.Success;
            }
            catch (Exception e)
            {
                response.Code = ResponseCode.Error;
                response.Msg = e.Message;
            }
            return response;
        }

        [HttpGet]
        [ActionName("insertInfo")]
        public ApiResponse Add(string warning)
        {
            ApiResponse response = new ApiResponse();
            WarningInfoEntity info = new WarningInfoEntity()
            {
                Warning = warning,
                Time = DateTime.Now.ToString()
            };
            var success = DataController.InsertWarningInfo(info);
            if (success != 0)
            {
                response.Code = ResponseCode.Success;
            }
            else
            {
                response.Code = ResponseCode.Error;
                response.Msg = "插入失败";
            }

            return response;
        }
    }
}
